<div class="layuimini-main">
    <div class="layui-form layuimini-form" lay-filter="updateForm">

        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="hidden" name="id" id="id">
                <input type="text" name="title" id="title" lay-verify="required" lay-reqtext="标题不能为空"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">内容</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" name="content" id="content" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">接收范围</label>
            <div class="layui-input-block building-update">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table','axios','ztree','laydate'], function () {
        let form = layui.form,
            layer = layui.layer,
            ztree = layui.ztree,
            laydate = layui.laydate,
            axios = layui.axios,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */

        //日期
        laydate.render({
            elem: '#visitTime'
        });
        laydate.render({
            elem: '#leaveTime'
        });

        // 当前弹出层，防止ID被覆盖
        let parentIndex = layer.index;


        function loadReceive(){
            return axios.post('noticeReceive/query',{noticeId:$('#id').val()});
        }

        function loadBuilding(){
            return axios.post('building/query',{});
        }

        axios.all([loadReceive(), loadBuilding()]).then(axios.spread(function (response, response2) {
                let arr = response.data.map(item=>item.buildingId);
                response2.data.forEach(item=>{
                    let option;
                    if(arr.some(val=>val == item.id)){
                        option = `<input type="checkbox" checked lay-skin="primary" name="buildingId" value="${item.id}">${item.name}<br/>`
                    }else{
                        option = `<input type="checkbox" lay-skin="primary" name="buildingId" value="${item.id}">${item.name}<br/>`
                    }
                    $('.building-update').append(option);
                })
                form.render();
        }));



        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let param = {};
            param.id = data.field.id;
            param.title = data.field.title;
            param.content = data.field.content;
            let arr = $('.building-update input[type="checkbox"]');
            let buildingIdArr = [];
            for(let i=0;i<arr.length;i++){
                if(arr[i].checked){
                    buildingIdArr.push(arr[i].value)
                }
            }
            param.buildingIds = buildingIdArr;
            axios.post('notice/update',param).then(function(response){
                if(response.code == 200){
                    layer.msg(response.msg);
                    window.reload();
                }
                layer.close(parentIndex);
            }).catch(function (error) {
                layer.msg(error);
            });
            return false;
        });

    });
</script>